package com.nb.hdj.baseClass;

import com.nb.hdj.controller.HelloController;
import com.nb.hdj.util.TestngListener;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Listeners;

import java.io.File;
import java.net.MalformedURLException;
/**
 * @Description: 创建Driver驱动基础类
 * @Author: hedongjia
 * @Date: 2021/11/3 下午5:35
 * @Version: v1.0
 */
public class CreateDriverBase {
    public WebDriver driver;
    private static final Logger logger = LoggerFactory.getLogger(CreateDriverBase.class);


    public WebDriver getDriver(){
        return driver;
    }

    public CreateDriverBase() throws MalformedURLException, InterruptedException {
        init();
    }

    /**
     * 初始化浏览器
     * 注释部分为链接selenium.hub的的代码，如需要可开启
     **/
    public void init() throws MalformedURLException, InterruptedException {
        /**
         System.out.println("start create driver");
         driver = new RemoteWebDriver(new URL("http://192.168.199.154:4444/wd/hub"), chromeCapabilities);
         System.out.println("driver create success");
         }
         */
        logger.info("------开始创建driver驱动------");
        //System.out.println("-----start create driver-----");
        //Windows和Linux 需要配置driver驱动路劲
        //System.setProperty("webdriver.chrome.driver", "./file/driverFile/chromedriver");
        ChromeOptions options = new ChromeOptions();
        //options.addArguments(new String[]{"test-type"});
        // 启动就最大化  覆盖全屏
        options.addArguments("start-fullscreen");
        options.addArguments("--start-maximized");
        // chrome正受到自动测试软件的控制
        //options.addArguments("disable-infobars");
        //设置忽略 Chrome 浏览器证书错误报警提示：
        options.addArguments("--test-type", "--ignore-certificate-errors");
        // 禁止默认浏览器检查
        options.addArguments("no-default-browser-check");
        options.addArguments("about:histograms");
        options.addArguments("about:cache");

        /**
         * 加载测试网页所需要的插件
         **/
//      loadingPlugIns(options,"插件路径");
        driver = new ChromeDriver(options);
        /**
         * 配置插件所需要执行的js代码
         **/
//      configurePlugs(driver,"这里是插件所配合要使用的js代码");
        logger.info("------创建driver驱动成功------");
        //driver.manage().window().maximize();
    }
    // }


    /**
     * 用来配置chrome的插
     * 可以用来执行js语言
     **/
    public void configurePlugs(WebDriver driver,String jsCode) throws InterruptedException {
        String js = "var scriptEle = document.createElement(\"script\");\n" +
                "  scriptEle.async = true;\n" +
                "  scriptEle.src = \"https://sharingan-resource.mastergo.com/static/js/app.js?v=\" + Date.now();\n" +
                "  document.body.appendChild(scriptEle);";
        JavascriptExecutor driver_js = ((JavascriptExecutor) driver);
        Thread.sleep(1000);
        driver_js.executeScript(jsCode);
    }

    /**
     * 加载插件
     * 如测试网页需要加载插件可以调用
     **/
    public void loadingPlugIns(ChromeOptions options,String filePath){
        options.addExtensions(new File(filePath));

    }

}
